package com.light.gamecomponents;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.text.ChangeableText;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;

import android.graphics.Color;
import android.graphics.Typeface;

import com.badlogic.gdx.math.Vector2;
import com.light.roller.Roller;

public class FormRunningText implements IGameComponents {

	protected String[] m_texts = { "_" };
	protected Texture m_fontTexture;
	protected ChangeableText m_printText;
	protected Roller m_game;
	protected Font m_font;
	protected Vector2 m_position;
	protected float m_totalTime = 0;
	protected int nowText = 0;
	protected Vector2 m_rotationCenter;

	public FormRunningText(Roller game, Vector2 position) {
		m_game = game;
		m_position = position;
	}

	@Override
	public void onLoadEngine() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onLoadResources() {
		this.m_fontTexture = new Texture(256, 256,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.m_font = new Font(this.m_fontTexture, Typeface.createFromAsset(m_game.getAssets(), "font/Arcade.ttf"), 29, true, Color.GREEN);
		m_game.getEngine().getTextureManager().loadTexture(this.m_fontTexture);
		m_game.getEngine().getFontManager().loadFont(this.m_font);
	}

	@Override
	public void unLoadResources() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onLoadScene(Scene scene, PhysicsWorld physicsWrold) {

		m_printText = new ChangeableText(m_position.x, m_position.y, m_font,
										 m_texts[0], 512);
		m_printText.setBlendFunction(GL10.GL_SRC_ALPHA,
									 GL10.GL_ONE_MINUS_SRC_ALPHA);
		scene.attachChild(m_printText);

	}

	@Override
	public void removeFromScene(Scene scene, PhysicsWorld physicsWorld) {
		scene.detachChild(m_printText);
		m_printText = null;
	}

	public void setPosition(Vector2 position) {
		m_position = position;
		m_printText.setPosition(position.x, position.y);
	}

	public void show() {
		m_printText.setText("");
		m_printText.setAlpha(1.0f);
		m_totalTime = 0.05f;
	}

	public void hide() {
		m_printText.setAlpha(0.0f);
	}

	@Override
	public void rotate(float angle) {
		setRotationCenter(m_rotationCenter);
		m_printText.setRotation(angle);
	}

	public void setRotationCenter(Vector2 center) {
		m_rotationCenter = center;
		m_printText.setRotationCenter(center.x, center.y);
	}
	
	public void showNextChar(float pSecondsElapsed)
	{
			
			String tempString;
			m_totalTime += pSecondsElapsed;
			
				int numberOfShowingChar = (int)(m_totalTime / 0.05f);
				if (numberOfShowingChar <= m_texts[nowText].length())
				{
					tempString = m_texts[nowText].substring(0, numberOfShowingChar) + "_";
				}
				else
				{
					int numberOfBlink = (int)(m_totalTime / 0.3f);
					if (numberOfBlink % 2 == 1)
						tempString = m_texts[nowText] + "_";
					else
						tempString = m_texts[nowText];
						
				}
				m_printText.setText(tempString);
				setRotationCenter(m_rotationCenter);
	}
	
	
	public void clearText()
	{
		m_printText.setText("");
	}
	
	public void setRunningText(String[] texts)
	{
		m_texts = texts;

	}
	public boolean ActionClicked()
	{
		
		int numberOfShowingChar = (int)(m_totalTime / 0.05f);
		if (numberOfShowingChar < m_texts[nowText].length())
		{
			m_totalTime = 1000;
			return false;
		}
		else if (nowText < m_texts.length - 1)
		{
			nowText += 1;
			m_totalTime = 0;
			return false;
		}
		else
		{
			nowText = 0;
			m_printText.setText("");
			return true;
		}
			
	}

}
